package com.woniu.cq.malluser.controller;


import com.woniu.cq.malluser.client.CommonClient;
import com.woniu.cq.mallcommon.common.JwtUntil;
import com.woniu.cq.malluser.common.AliyunMessage;
import com.woniu.cq.malluser.common.RandomUtil;
import com.woniu.cq.malluser.common.Result;
import com.woniu.cq.malluser.params.CodeParam;
import com.woniu.cq.malluser.service.IUserService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author daniel
 * @since 2020-09-19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private CommonClient commonClient;

    @Autowired
    private IUserService userService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /** 
     * @description:  
     * @param: mobile 
     * @return: com.woniu.cq.malluser.common.Result 
     */
    @GetMapping("/getCode")
    public Result getCode(String mobile) {
        String code = commonClient.getCode(mobile);
        return Result.builder().code(200).meg("获取成功").data(code).build();
    }
    /*@GetMapping("/getCode")
    public Result getCode(String phone){
        AliyunMessage aliyunMessage = new AliyunMessage();
        String code = RandomUtil.randomCode(6);
        try {
            String codes = aliyunMessage.sendSms(phone, code);
            return Result.builder().code(200).data(codes).meg("成功").build();
        }catch (Exception e){
            e.printStackTrace();
            return Result.builder().meg("失败").build();
        }
    }*/
    /** 
     * @description:  
     * @param: codeParam 
     * @return: com.woniu.cq.malluser.common.Result 
     */
    @PutMapping("/register")
    public Result register(CodeParam codeParam) {
        try {
            userService.register(codeParam);
            return Result.builder().meg("注册成功").code(200).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.builder().code(500).meg("注册失败,验证码错误").build();
        }
    }
    /**
     * 短信producer
     * @return
     */
    @GetMapping("/sendMsg/{tel}")
    public SendResult sendMessage(@PathVariable String tel){
        Message<String> msg = MessageBuilder.withPayload(tel)
                //设置producer的key
                .setHeader("KEYS", UUID.randomUUID().toString())
                .build();
        SendResult sr = rocketMQTemplate.syncSend("topic:code", msg);
        return sr;
    }
    
    /**
     * @description: 登录
     * @param:
     * @return: java.lang.String
     */
    @PostMapping("/login")
    public String login(String name,String pass){
        Map<String, Object> maps = new HashMap<>();
        maps.put("name",name);
        maps.put("sex","boy");
        return JwtUntil.createToken(maps,5*60*60);
    }
}

